{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit TextPatternOptions;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Scene, superobject, Dispatcher, VisualUtils, Transmitter, MiscUtils;

type
  TTextPatternOptionsForm = class(TForm)
    ApplicationProperties: TApplicationProperties;
    btnCancel: TButton;
    btnOk: TButton;
    cbWildcard: TCheckBox;
    cbDetectNumbers: TCheckBox;
    cbSpaceDelimited: TCheckBox;
    cbIgnoreCase: TCheckBox;
    edtPrecision: TEdit;
    gbxMain: TGroupBox;
    lblPrecision: TLabel;
    pnlButtons: TPanel;
    pnlPrecision: TPanel;
    procedure ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
    procedure btnOkClick(Sender: TObject);
    procedure edtPrecisionChange(Sender: TObject);
  private
    FId: TShapeId;
    FOptions: ISuperObject;
    FDispatcher: TDispatcher;
    procedure Prepare;
    { private declarations }
  public
    class procedure OpenModal(const Id: TShapeId; const Options: ISuperObject; Dispatcher: TDispatcher);
    { public declarations }
  end; 

implementation

{$R *.lfm}

{ TTextPatternOptionsForm }

procedure TTextPatternOptionsForm.ApplicationPropertiesIdle(Sender: TObject;
  var Done: Boolean);
begin
  edtPrecision.Enabled := cbDetectNumbers.Checked;
  lblPrecision.Enabled := cbDetectNumbers.Checked;
end;

procedure TTextPatternOptionsForm.btnOkClick(Sender: TObject);
var
  Input: ISuperObject;
begin
  Input := SO;
  Input.B['wildcard'] := cbWildcard.Checked;
  if cbDetectNumbers.Checked then
    Input.S['precision'] := CommaToDot(Trim(edtPrecision.Text));
  Input.B['spaceDelimited'] := cbSpaceDelimited.Checked;
  Input.B['caseSensitive'] := not cbIgnoreCase.Checked;

  try
    FDispatcher.PerformAction(FId, 'setOptions', Input);
    ModalResult := mrOk;
  except on E: ECommandFailed do
    begin
      SetEditError(edtPrecision);
      TrySetFocus(edtPrecision);
    end;
  end;
end;

procedure TTextPatternOptionsForm.edtPrecisionChange(Sender: TObject);
begin
  ResetEditError(edtPrecision);
end;

procedure TTextPatternOptionsForm.Prepare;
var
  Precision: ISuperObject;
begin
  edtPrecision.MaxLength := FOptions.I['maxPrecisionSize'];
  SetEditWidthByMaxLength(edtPrecision);

  cbWildcard.Checked := FOptions.B['wildcard'];

  Precision := FOptions['precision'];
  if Precision = nil then
    edtPrecision.Text := '0'
  else
  begin
    cbDetectNumbers.Checked := TRUE;
    edtPrecision.Text := DotToUiDecimalSeparator(UTF8Encode(Precision.AsString));
  end;

  cbSpaceDelimited.Checked := FOptions.B['spaceDelimited'];
  cbIgnoreCase.Checked := not FOptions.B['caseSensitive'];
end;

class procedure TTextPatternOptionsForm.OpenModal(const Id: TShapeId; const Options: ISuperObject; Dispatcher: TDispatcher);
var
  Form: TTextPatternOptionsForm;
begin
  Form := TTextPatternOptionsForm.Create(nil);
  try
    RestoreFormPosition(Form);

    Form.FId := Id;
    Form.FOptions := Options;
    Form.FDispatcher := Dispatcher;
    Form.Prepare;

    ShowModalForm(Form);
  finally
    Form.Free;
  end;
end;

end.

